package com.example.config.security.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.config.security.entity.JwtUser;
import com.example.entity.User;
import com.example.service.IUserService;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;

@Configuration
public class SelfUserDetailsService implements UserDetailsService {
    @Resource
    IUserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User one = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getUsername, username));
        if (null!=one){
            return new JwtUser(one);
        }

        throw new UsernameNotFoundException("用户名或密码不正确");
    }
}
